package com.xcdh.util;

import com.xcdh.domain.vo.DeptTreeVo;
import com.xcdh.domain.vo.ElTree;

import java.util.*;

/**
 * TreeUtil
 * Created by wjg on 2024/1/29.
 */
public class TreeUtil {

    public static <T> List<ElTree<T>> buildTree(Collection<? extends ElTree<T>> list) {
        List<ElTree<T>> result = new ArrayList<>();
        Map<T, List<ElTree<T>>> treeMap = new HashMap<>();

        for (ElTree<T> elTree : list) {
            T parentId = elTree.getParentId();
            treeMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(elTree);
        }

        for (ElTree<T> elTree : list) {
            T id = elTree.getId();
            List<ElTree<T>> elTreeList = treeMap.remove(id);
            elTree.setChildren(elTreeList);
        }

        treeMap.values().forEach(result::addAll);

        return result;
    }



}
